package com.e1xp.deport_management_system.controller;


import com.e1xp.deport_management_system.entity.Product;
import com.e1xp.deport_management_system.exception.CustomException;
import com.e1xp.deport_management_system.service.IProductService;
import com.e1xp.deport_management_system.utils.AjaxResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    IProductService productService;

    /**
     * 渲染商品管理页面数据
     *
     * @return ModelAndView
     */
    @RequestMapping("/productData")
    public ModelAndView productManager(@SessionAttribute long instanceId) {
        ModelAndView modelAndView = new ModelAndView();
        //获取系列数据
        List<Product> productList = productService.getList(instanceId);
        //置入数据
        modelAndView.addObject("productList", productList);
        modelAndView.setViewName("productManager");
        return modelAndView;
    }

    /**
     * 用于渲染添加产品页面
     *
     * @return ModelAndView
     */
    @RequestMapping("/addProductPage")
    public ModelAndView addProductPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("addProduct");
        return modelAndView;
    }

    /**
     * 用于添加新商品
     *
     * @param instanceId   实例id
     * @param productId    商品码
     * @param productPrice 价格
     * @param productName  商品名
     * @return AjaxResponse
     * @throws CustomException 当实例id不存在、商品码重复时抛出该异常
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResponse add(@SessionAttribute long instanceId, long productId, double productPrice, String productName) throws CustomException {
        Product product = new Product(productId, productName, 0L, 0L, productPrice, instanceId);
        productService.add(product);
        return AjaxResponse.success();
    }

    /**
     * 用于获取商品选择列表
     *
     * @param instanceId 实例id
     * @return AjaxResponse
     */
    @PostMapping("/getSelection")
    @ResponseBody
    public AjaxResponse getSelection(@SessionAttribute long instanceId) {
        List<Map<String, Object>> selectList = productService.getSelection(instanceId);
        return AjaxResponse.success(selectList);
    }

    /**
     * 用于更新产品
     *
     * @param instanceId 实例id
     * @param product    产品实体
     * @return AjaxResponse
     * @throws CustomException 当实例不存在，产品信息不完整时抛出该异常
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResponse update(@SessionAttribute long instanceId, Product product) throws CustomException {
        productService.updateProduct(instanceId, product);
        return AjaxResponse.success();
    }

    /**
     * 用于渲染产品详情页面
     *
     * @param instanceId 实例id
     * @param productId  产品id
     * @return ModelAndView
     */
    @RequestMapping("/detail")
    public ModelAndView detail(@SessionAttribute long instanceId, long productId) {
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.get(instanceId, productId);
        if (product == null) {
            modelAndView.setViewName("redirect:/401Page.html");
        } else {
            modelAndView.addObject("product", product);
            modelAndView.setViewName("changeProduct");
        }
        return modelAndView;
    }
}
